package com.example.demo.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Object;
import com.example.demo.entity.Store;
import com.example.demo.mapper.ObjectMapper;
import com.example.demo.mapper.StoreMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController//用来访问json数据
@RequestMapping("/object")//定义统一路由
public class ObjectController {
    @Resource
    ObjectMapper objectMapper;


    @PostMapping//post新增
    public Result<?> save(@RequestBody Object object){
//        System.out.println(store.getStoreName());
//        System.out.println(store.getRemark());

        //insert已经封装好了不用知道细节
        objectMapper.insert(object);

        return Result.success();
    }
    @DeleteMapping("/{id}")//删除
    public Result<?> save(@PathVariable Long id){

        objectMapper.deleteById(id);
        return Result.success();
    }

    @PutMapping//更新
    public Result<?> update(@RequestBody Object object){


        objectMapper.updateById(object);
        return Result.success();
    }
    @GetMapping//查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,//defaultValue设置默认页数
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Object> wrapper = Wrappers.<Object>lambdaQuery().like(Object::getObjName, search);

        if(StrUtil.isNotBlank(search)){
            wrapper.like(Object::getObjName,search);
        }
        Page<Object> storePage = objectMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(storePage);
    }
}
